﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace XnaSuperGame	
{
	public class MenuButton
	{
		private Vector2 Position;
		private Vector2 Size;
		private Color Color = new Color(255, 255, 255, 255);

		private Texture2D Texture;

		public bool IsClicked;
		private bool Down;
		private GameStates State;
		
		private Rectangle RectBox;


		public MenuButton(GameWindow gwin, Texture2D newTexture,GameStates targetState)
		{
			Texture = newTexture;
			State = targetState;
			//Set default position associated with GraphicslDevice.Viewport 
			Position = new Vector2(
				(float)gwin.ClientBounds.Center.X
				, (float)gwin.ClientBounds.Center.Y);

			Size = new Vector2(
				(float)newTexture.Width
				, (float)newTexture.Height);

			//Set default state for button
			IsClicked = false;

			//Set default Rectangle-box associated with newTexture
			RectBox = new Rectangle(
				(int)gwin.ClientBounds.Center.X
				, (int)gwin.ClientBounds.Center.Y
				, newTexture.Width
				, newTexture.Height);
		}




		public void Update(MouseState mouse)
		{
			UpdateRectBox();
			var mouseBox = new Rectangle(mouse.X, mouse.Y, 1, 1);

			if (mouseBox.Intersects(RectBox))
			{
				if (Color.B == 255) Down = false;
				if (Color.B == 0) Down = true;
				if (Down) Color.B += 3; else Color.B -= 3;
				if (mouse.LeftButton == ButtonState.Pressed) IsClicked = true;
			}
			else if (Color.B < 255) 
			{
				Color.B += 3;
				IsClicked = false;
			}

		}

		public void Draw(SpriteBatch spriteBatch) 
		{
			var rect = new Rectangle (
					(int)Position.X
				,	(int)Position.Y
				,	Texture.Width
				,	Texture.Height);

			spriteBatch.Draw(Texture, rect, Color.White);
		}
		public void SetPosition(Vector2 newPosition)
		{
			Position = newPosition;
			//RectBox.X = new_X;
			//RectBox.Y = new_Y;
		}

		public void SetTexture(Texture2D newTexture)
		{
			Texture = newTexture;

			Size.X = (float)newTexture.Width;
			Size.Y = (float)newTexture.Height;
			//RectBox.Width = newTexture.Width;
			//RectBox.Height = newTexture.Height;
		}

		public void UpdateRectBox()
		{
			RectBox.X = (int)Position.X;

			RectBox.Height = (int)Size.Y;
		}

		public GameStates TargetState() { return State; }
	}
}
